package test;

import java.util.*;

public class ParamsUtil {
	/**
	   * 除去数组中的空值和签名参数
	   * 
	   * @param sArray 签名参数组
	   * @param bRemoveBlank bRemoveBlank true 去掉空值，false 不过滤空值
	   * @return 去掉空值与签名参数后的新签名参数组
	   */
		public static Map<String, String> paraFilter(Map<String, String> sArray, boolean bRemoveBlank) {

		Map<String, String> result = new HashMap<String, String>();

			if (sArray == null || sArray.size() <= 0) {
				return result;
			}

			for (String key : sArray.keySet()) {
				String value = sArray.get(key);
				if (bRemoveBlank){
					if (value == null || value.equals("") ) {
						continue;
					}
				}

				if (key.equalsIgnoreCase("sign")
					|| key.equalsIgnoreCase("sign_type")) {
					continue;
				}
				result.put(key, value);
			}

		return result;
		}

	  /**
	   * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
	   * 
	   * @param params 需要排序并参与字符拼接的参数组
	   * @return 拼接后字符串
	   */
	  public static String createLinkString(Map<String, String> params) {

	    List<String> keys = new ArrayList<String>(params.keySet());
	    Collections.sort(keys);

	    String prestr = "";

	    for (int i = 0; i < keys.size(); i++) {
	      String key = keys.get(i);
	      String value = params.get(key);

	      if (i == keys.size() - 1) {// 拼接时，不包括最后一个&字符
	        prestr = prestr + key + "=" + value;
	      } else {
	        prestr = prestr + key + "=" + value + "&";
	      }
	    }

	    return prestr;
	  }
}
